<?php

namespace App\Http\Controllers\Admin;

use App\Models\Category;

class CategoryController extends BaseController
{

    public function __construct(Category $category)
    {
        parent::__construct();
        $this->category = $category;
    }


    public function getIndex()
    {
        $order['sort'] = 'asc' ;
        $res = $this->category->getList($where=[], '*', $order, '')->toArray();
        $rows = '';
        if ($res) {
            $rows = childTree($res, 0, 'id', 'parentid');
        }
        $flag = 'category';

        //dd($rows);
        return view('admin.category.index', compact('rows','flag'));
    }

    public function getAdd()
    {
        $res = $this->category->getList()->toArray();
        $rows = '';
        if ($res) {
            $rows = childTree($res, 0, 'id', 'parentid');
        }
        $flag = 'category';
        //dd($rows);
        return view('admin.category.add', compact('rows','flag'));
    }


    public function postAdd()
    {
        //dd($_POST);
        $data = request()->except('_token');
        $condition['name'] = $data['name'];
        $checkTitle = $this->category->getOne($condition);
        if ($checkTitle) {
            return back()->with('message', '该分类标题已存在');
        }
        $this->category->add($data);
        return redirect()->back()->with('message', '添加成功！')->with('status',1);

    }


    public function getEdit()
    {

        $data = $this->category->getOne(['id'=>request('id')])->toArray();
        $list = $this->category->getList()->toArray();
        $rows = childTree($list, 0, 'id', 'parentid');
//        $category = '';
//        foreach ($rows as $v) {
//            if ($v['id'] == request('id')) {
//                $category = $v;
//            }
//        }
        $flag = 'category';
        return view('admin.category.edit', compact('rows','data','flag'));
    }

    public function postEdit()
    {
        //dd(request());
        $where['id'] = request('id');
        $data = request()->except(['_method', '_token']);
        $condition['name'] = $data['name'];
        $condition['id'] = ['<>', $where['id']];
        $checkTitle = $this->category->getOne($condition);
        if ($checkTitle) {
            return back()->with('message', '该分类标题已存在');
        }
        $did = $this->category->up($where, $data);

        if(!$did){
            return redirect()->back()->with('message', '系统繁忙,请稍后再试');
        }
        return redirect()->back()->with('message', '修改成功!')->with('status',1);
    }

    //ajax改变显示状态
    public function postChange()
    {
        //dd($_POST);
        $where['id'] = request('id');
        $data['show'] = request('show');
        $did = $this->category->up($where, $data);
        if ($did) {
            $res['status'] = 1;
            $res['msg'] = '修改成功!';

        }else{
            $res['status'] = 0;
            $res['msg'] = '修改失败!';
        }
        return ajax_return($res);
    }

    public function postSort()
    {
        $where['id'] = request('id');
        $data['sort'] = request('sort');
        $res = $this->category->up($where, $data);
        if ($res) {
            echo 1;
            exit;
        }
        echo 0;
        exit;
    }

    public function getChild()
    {
        $res = $this->category->getChild(request());
        return ajax_return($res);
    }

    //删除分类
    public function getDel()
    {

        $ab = $this->category->getOne(['parentid'=>request('id')]);   //如果有一条数据的父级id为此分类id则证明,此分类下有子分类,不能删除
        if($ab){
            $res['status'] = 0;
            $res['msg'] = '改分类下有子分类,无法删除';
            return ajax_return($res);
        }


        $where['id'] = request('id');
        $did = $this->category->del($where);
        if($did){
            $res['status'] = 1;
            $res['msg'] = '删除分类成功';
        }else{
            $res['status'] = 0;
            $res['msg'] = '删除分类失败';
        }
        return ajax_return($res);
    }

}
